﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace TestDownloadUrl {
    class Program {
        static void Main(string[] args) {
            string WebUrl = "http://www.baidu.com";
            string SaveWebpagePath = "D:/test/";
            HttpWebRequest Req = (HttpWebRequest)WebRequest.Create(new System.Uri(WebUrl));

            Req.UserAgent = "Mozilla/4.0(compatible;MSIE 6.0;Windows NT 5.0; .NET CLR 1.1.4322)";
            Req.Timeout = 30000;

            try {
                Console.WriteLine("Start Send Request...");
                StreamReader responseReader = new StreamReader(Req.GetResponse().GetResponseStream(), Encoding.Default);
                string responseData = responseReader.ReadToEnd();
                responseReader.Close();
                Console.WriteLine("Close Request...");

                Console.WriteLine("Start Write Response To File...");
                StreamWriter sw = new StreamWriter(SaveWebpagePath + "test.html", false, Encoding.Default);
                sw.Write(responseData);
                sw.Close();
                Console.WriteLine("Write Response To File Success!");
            } catch (Exception ex) {
                Console.WriteLine("Response Failure!");
                Console.WriteLine(ex);
            }
        }
    }
}
